package com.wallet.crypto.trustapp.ui.widget.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wallet.crypto.trustapp.R;
import com.wallet.crypto.trustapp.ui.widget.entity.SettingItem;
import com.wallet.crypto.trustapp.ui.widget.holder.SettingHolder;
import com.wallet.crypto.trustapp.ui.widget.holder.SettingLineHolder;


import java.util.ArrayList;

/**
 * Created by lingjianzhong on 2018/5/8.
 */

public class SettingAdapter extends RecyclerView.Adapter<SettingHolder> implements View.OnClickListener {

    ArrayList<SettingItem> mItems;
    Context mContext;
    LayoutInflater mInflater;

    private static final int NORMAL_TYPE = 0;
    private static final int LINE_TYPE = 1;

    public SettingAdapter(Context context, ArrayList<SettingItem> mItems) {
        this.mContext = context;
        this.mItems = mItems;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public SettingHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == NORMAL_TYPE) {
            View itemView = mInflater.inflate(R.layout.item_setting_rev, parent, false);
            itemView.setOnClickListener(this);
            return new SettingHolder(itemView);
        } else {
            View itemView = mInflater.inflate(R.layout.item_setting_rev_line, parent, false);
            return new SettingLineHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull SettingHolder holder, int position) {
        if (!(holder instanceof SettingLineHolder)) {
            holder.mIcon.setImageResource(mItems.get(position).getIcon());
            holder.mTitle.setText(mItems.get(position).getTitle());
            holder.itemView.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        boolean b = mItems.get(position).isShowLine();
        if (b)
            return LINE_TYPE;
        else
            return NORMAL_TYPE;
    }

    private OnItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null)
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
